'''
    设g(x)=(x-1)**2,则dg(x)/dx=2x-2
    使用最速下降法求g(x)最低点
'''

# def g(t):
#     return (t-1)**2
Maxtimes = 100

def d_g(t):
    return 2*t-2

x = 0
eta = 0
while(x != '#'):
    try:
        temp = input("请输入x初始值，#号结束程序：")
        if temp == "#":
            print("感谢使用，再见！")
            break
        else:
            x = float(temp)
        temp = input("请输入eta初始值：")
        eta = float(temp)
    except ValueError:
        print("请勿输入数字、#号以外的字符")
    vodg = d_g(x)
    flag = 0
    while(abs(vodg)>0.0001 and flag<Maxtimes):
        x = x - eta*vodg
        vodg = d_g(x)
        flag += 1
        print(f"迭代第{flag}次，当前x值为{x}")
    print(f"迭代{flag}次\n找到最低点x值为{x}")
